package com.apex.common.controller;

import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-4-8
 */
public abstract class BaseService {

    public <T> void getListObject(ModelMap modelMap, String value, Class<T> t, String mapKey) {
        List<T> list = null;
        if (!StringUtils.isEmpty(value)) {
            Any any = JsonIterator.deserialize(value);
            if ("true".equals(any.get("result").toString())) {
                List<Any> anyList = any.get("dataList").asList();
                if (null != anyList && anyList.size() > 0) {
                    list = new ArrayList<T>();
                    for (Any entity : anyList) {
                        list.add(entity.as(t));
                    }
                }
            }
        }
        modelMap.put(mapKey, list);
    }

    public <T>List<T> getAllListObject(Class<T> t,String value){
        return getAllListObject(t,value,null);
    }
    public <T>List<T> getAllListObject(Class<T> t,String value,String dataName){
        List<T> list=null;
        if(!StringUtils.isEmpty(value)){
            Any any=JsonIterator.deserialize(value);
            if("true".equals(any.get("result").toString())){
                List<Any> anyList=null;
                if(StringUtils.isEmpty(dataName)){
                    anyList= any.get("dataList").asList();
                }else {
                    anyList= any.get(dataName).asList();
                }
                if(null!=anyList&&anyList.size()>0){
                    list=new ArrayList<>();
                    for (Any entity: anyList){
                        list.add(entity.as(t));
                    }
                }
            }
        }
        return list;
    }


    public <T> void getObject(ModelMap modelMap, String value, Class<T> t, String mapKey) {
        T t1 = null;
        if (!StringUtils.isEmpty(value)) {
            Any any = JsonIterator.deserialize(value);
            if ("true".equals(any.get("result").toString())) {
                Any anyList = any.get("data");
                t1 = anyList.as(t);
            }
        }
        modelMap.put(mapKey, t1);
    }

    public <T> T getObject(Class<T> t,String value){
        T t1=null;
        if (!StringUtils.isEmpty(value)) {
            Any any = JsonIterator.deserialize(value);
            if ("true".equals(any.get("result").toString())) {
                Any anyList = any.get("data");
                t1 = anyList.as(t);
            }
        }
        return t1;
    }

    public <T> T getObjectByNoData(Class<T> t,String value){
        if(StringUtils.isEmpty(value)){
            Any any=JsonIterator.deserialize(value);
            return any.as(t);
        }
        return null;
    }

    public <T> T getObjectByListOneDate(Class<T> t,String value){
        if(!StringUtils.isEmpty(value)){
            Any any= JsonIterator.deserialize(value);
            if(null!=any){
                List<Any> list=any.asList();
                if(null!=list&&list.size()>0){
                    Any any1=list.get(0);
                    T t1=any1.as(t);
                    return t1;
                }
            }
        }
        return null;
    }

}

